package com.taichuan.keysupport.panel;

import android.content.Context;
import android.util.Log;

import com.taichuan.keysupport.Key;
import com.taichuan.keysupport.R;
import com.taichuan.keysupport.keyset.KeySet;
import com.taichuan.keysupport.keyset.T12KeySet;

/**
 * Created by OISNY on 2016/8/19.
 */
public class T12KeyBoardPanelManager extends PanelManager {
    private static final String TAG = T12KeyBoardPanelManager.class.getSimpleName();

    public T12KeyBoardPanelManager(Context context) {
        super(context);
    }

    @Override
    public int getPanelLayoutResource() {
        return R.layout.t12key_bord;
    }

    @Override
    public int getPanelItemResource() {
        return R.layout.t12key_text;
    }

    @Override
    public KeySet initKeySet() {
        return new T12KeySet(this);
    }

    @Override
    public void onKeyDown(int keyCode) {
        Log.i(TAG, "onKeyDown: 接收到按键事件 " + keyCode);
        if (keyCode == Key.Convert.KEYCODE_XING) { // *
            setEditText("-1");
            oldKeyIndex = -1;
        } else if (keyCode == Key.Convert.KEYCODE_JING) { // #
            mKeySet.performClickJ();
            oldKeyIndex = -1;
        } else if (keyCode == Key.Convert.KEYCODE_0 && isDispatchZero()) { // 0
            mKeySet.performClickZero();
        } else if (keyCode == Key.Convert.KEYCODE_LONG_JING
                || keyCode == Key.Convert.KEYCODE_LONG_0
                || keyCode == Key.Convert.KEYCODE_LONG_XING
                || keyCode == Key.Convert.KEYCODE_OK
                || keyCode == Key.Convert.KEYCODE_TAB
                || keyCode == Key.Convert.KEYCODE_GLZHX
                ) {
        }else if(keyCode == Key.Convert.KEYCODE_LONG_OK
                || keyCode == Key.Convert.KEYCODE_LONG_TAB
                || keyCode == Key.Convert.KEYCODE_LONG_GLZHX) {
            // 美关公项目暂时重新判断 人脸、开锁、帮助按键的长按逻辑处理
        }

        else {
            this.keyIndex = getKeyIndex(keyCode);
            if (oldKeyIndex == this.keyIndex) {
                if (mEditText != null) {
                    mEditText.removeCallbacks(this);
                    isSameKeyIndex = true;
                    mEditText.postDelayed(this, 2000);
                }
            } else {
                isSameKeyIndex = false;
            }
            mKeySet.performKey(this.keyIndex, isSameKeyIndex);
            oldKeyIndex = this.keyIndex;
        }
    }

    boolean isDispatchZero() {
        return jIndex != 2;
    }

    @Override
    protected boolean isDelayInput() {
        return jIndex != 0;
    }
}